﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;
using DevExpress.Web.Mvc;
using System.Data;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 工作组管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class GroupController : AdminBaseController
    {
        /// <summary>
        /// 工作组管理
        /// </summary>
        /// <returns>返回工作组管理首主页面视图</returns>
        [AuthorityActionFilter("m_HRGroup", Order = 1)]
        [LogPageActionFilter("工作组管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 工作组Tree 初始化
        /// </summary>
        /// <returns>返回工作组管理左边树视图</returns>
        public ActionResult LeftTree()
        {
            TreeModelList treeModelList = new TreeModelList();

            TreeItemModel treeRootItemModel = new TreeItemModel(null)
            {
                Id = Url.Action("GroupList","Group"),
                Name = "工作组",
                ImageUrl = Url.Content("~/Content/ControlImages/users.png")
            };
            BLL.HRWorkGrp hRWorkGrpbll = new BLL.HRWorkGrp();
            List<Model.HRWorkGrp> list = hRWorkGrpbll.GetModelList("FEntityNo='" + GlobalConstant.ENTITYNO + "'");
            foreach (Model.HRWorkGrp model in list)
            {
                TreeItemModel treeParentItemModel = new TreeItemModel(treeRootItemModel)
                {
                    Id = Url.Action("GroupEmplList", "Group", new { type = "group", gNo = model.FWorkGrpNo }), 
                    Name = model.FWorkGrpName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };
                LoadDutyTreeNode(treeParentItemModel, model.FWorkGrpNo);

            }
            treeModelList.Add(treeRootItemModel);
            return View(treeModelList);
        }

        
        /// <summary>
        /// 加载每个工作组下的职务
        /// </summary>
        /// <param name="node">父级树节点</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        private void LoadDutyTreeNode(TreeItemModel node, string fWorkGrpNo)
        {
            BLL.HRWorkGrpDuty wgDutyBll = new BLL.HRWorkGrpDuty();
            List<Model.HRWorkGrpDuty> list = wgDutyBll.GetModelList("FWorkGrpNo='" + fWorkGrpNo + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "' order by FSeq asc");
            foreach (Model.HRWorkGrpDuty model in list)
            {
                TreeItemModel treeParentItemModel = new TreeItemModel(node)
                {
                    Id = Url.Action("GroupEmplList", "Group", new { type = "duty",gNo=fWorkGrpNo, dutyNo = model.FWorkGrpDutyNo }),
                    Name = model.FDutyName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

            }
        }
        /// <summary>
        /// 工作组Grid
        /// </summary>
        /// <returns>返回工作组Grid操作后视图</returns>
        public ActionResult GroupListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string deptNo = Request.Params["deptNo"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }


            List<Model.HRWorkGrp> hRWorkGrpList = GetHRWorkGrpList(pageIndex, pageRowSize);


            return PartialView("GroupListPartial", hRWorkGrpList);
        }
       /// <summary>
       ///工作组列表页初始化
       /// </summary>
       /// <returns>返回工作组列表视图</returns>
        public ActionResult GroupList()
        {
            int pageIndex = 1;

            List<Model.HRWorkGrp> hRWorkGrpList = GetHRWorkGrpList(pageIndex);

            return View(hRWorkGrpList);
        }

        /// <summary>
        /// 工作组人员列表页初始化
        /// </summary>
        /// <param name="type">状态 duty为岗位 group为工作组</param>
        /// <param name="gNO">工作组编号</param>
        /// <param name="dutyNo">岗位编号</param>
        /// <returns>返回工作组人员列表视图</returns>
        public ActionResult GroupEmplList(string type, string gNO, string dutyNo)
        {
            ViewData["type"] = type;
            ViewData["gNO"] = gNO;
            ViewData["dutyNo"] = dutyNo;
            string strWhere = "";
            if (type == "duty")
            {
                strWhere = "FWorkGrpDutyNo='" + dutyNo + "' and FWorkGrpNo='" + gNO + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'";
            }
            else
            {
                strWhere = "FWorkGrpNo='" + gNO + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'";
            }

            int pageIndex = 1;

            List<Model.HRWorkGrpMember> hRWorkGrpMemberList = GetGroupDutyEmplList(strWhere, pageIndex);

            return View(hRWorkGrpMemberList);
        }
        /// <summary>
        /// 工作组人员grid
        /// </summary>
        /// <returns>返回工作组人员Grid操作后视图</returns>
        public ActionResult GroupEmplListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());

            string type = Request.Params["Type"];
            string gNO = Request.Params["gNO"];
            string dutyNo = Request.Params["DutyNo"];

            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strWhere = "";
            if (type == "duty")
            {
                strWhere = "FWorkGrpDutyNo='" + dutyNo + "' and FWorkGrpNo='" + gNO + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'";
            }
            else
            {
                strWhere = "FWorkGrpNo='" + gNO + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'";
            }
            List<Model.HRWorkGrpMember> hRWorkGrpList = GetGroupDutyEmplList(strWhere, pageIndex, pageRowSize);


            return PartialView("GroupEmplListPartial", hRWorkGrpList);
        }


        /// <summary>
        /// 获取工作组下人员列表
        /// </summary>
        /// <param name="strwhere">查询条件</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageRowSize">每页显示条数</param>
        /// <returns>返回工作组下人员列表</returns>
        private List<Model.HRWorkGrpMember> GetGroupDutyEmplList(string strwhere ,int pageIndex, int pageRowSize = 0)
        {
            BLL.Common commonBll = new BLL.Common();

            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("v_HRWorkGrpMember", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strwhere, "FWorkGrpNo", 0, out total);

            BLL.HRWorkGrpDuty hRWorkGrpDutyBll = new BLL.HRWorkGrpDuty();
            List<Model.HRWorkGrpMember> hRWorkGrpAllList = new List<Model.HRWorkGrpMember>();
            List<Model.HRWorkGrpMember> hRWorkGrplList = hRWorkGrpDutyBll.DataTableToHRWorkGrpMemberList(ds.Tables[0]);
            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                hRWorkGrpAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (hRWorkGrplList.Count > i)
                {
                    hRWorkGrpAllList[((pageIndex - 1) * pageRowSize) + i] = hRWorkGrplList[i];
                }
            }

            return hRWorkGrpAllList;
        }


        /// <summary>
        /// 获取工作组集合
        /// </summary>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageRowSize">每页显示条数</param>
        /// <returns>工作组集合</returns>
        private List<Model.HRWorkGrp> GetHRWorkGrpList(int pageIndex, int pageRowSize = 0)
        {
            BLL.Common commonBll = new BLL.Common();

            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("t_HRWorkGrp", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "FWorkGrpNo", 0, out total);

            BLL.HRWorkGrp hRWorkGrpBll = new BLL.HRWorkGrp();
            List<Model.HRWorkGrp> hRWorkGrpAllList = new List<Model.HRWorkGrp>();
            List<Model.HRWorkGrp> hRWorkGrplList = hRWorkGrpBll.DataTableToList(ds.Tables[0]);
            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                hRWorkGrpAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (hRWorkGrplList.Count > i)
                {
                    hRWorkGrpAllList[((pageIndex - 1) * pageRowSize) + i] = hRWorkGrplList[i];
                }
            }

            return hRWorkGrpAllList;
        }

        /// <summary>
        /// 工作组增加修改页面初始化
        /// </summary>
        /// <param name="gNo">工作组编号</param>
        /// <returns>增加或修改工作组视图</returns>
        public ActionResult GroupEdit(string gNo)
        {
            Model.HRWorkGrp hRWorkGrpModel = new Model.HRWorkGrp();
            BLL.HRWorkGrp hRWorkGrpBLL = new BLL.HRWorkGrp();
            if (gNo != "" && gNo!=null)
            {
                hRWorkGrpModel = hRWorkGrpBLL.GetModel(GlobalConstant.ENTITYNO, gNo);
                ViewData["hdMethod"] = "Edit";
                BLL.HRWorkGrpDuty workGrpDutyBll = new BLL.HRWorkGrpDuty();
                List<Model.HRWorkGrpDuty> hRWorkGrpDutyList = workGrpDutyBll.GetModelList("FWorkGrpNo='" + gNo + "' and FEntityNo='" + GlobalConstant.ENTITYNO + "' order by FSeq asc ");

                string _jsonstr = "";
                _jsonstr += JsonConvert.SerializeObject(hRWorkGrpDutyList);
                ViewData["HRWorkGrpDutyList"] = _jsonstr;
                ViewData["HRWorkGrpDutyNewList"] = "";
                ViewData["blemp"] = false;
            } 
            else
            {
                hRWorkGrpModel.FIsUse = "Y";
                ViewData["hdMethod"] = "ADD";
                ViewData["HRWorkGrpDutyList"] = "";
                ViewData["HRWorkGrpDutyNewList"] = "";
                ViewData["blemp"] = true;
            }
           
            return View(hRWorkGrpModel);
        }
        /// <summary>
        /// 工作组人员增加修改页面初始化
        /// </summary>
        /// <param name="gNo">工作组编号</param>
        /// <param name="fEmplNO">人员编号</param>
        /// <returns>工作组人员增加或修改视图</returns>
        public ActionResult GroupEmplEdit(string gNo, string fEmplNO)
        {
            Model.HRWorkGrpMember hRWorkGrpMemberModel = new Model.HRWorkGrpMember();
            BLL.HRWorkGrpDuty workGrpDutyBll = new BLL.HRWorkGrpDuty();
            List<Model.HRWorkGrpDuty> hRWorkGrpDutyList = workGrpDutyBll.GetModelList("FWorkGrpNo='" + gNo + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'");
            ViewData["HRWorkGrpDutyList"] = hRWorkGrpDutyList;
            if (fEmplNO != "" && fEmplNO != null)
            {
                BLL.HRWorkGrpMember hRWorkGrpMemberBLL = new BLL.HRWorkGrpMember();
                hRWorkGrpMemberModel = hRWorkGrpMemberBLL.GetModel(Common.GlobalConstant.ENTITYNO, gNo, fEmplNO);
                ViewData["isVisible"] = false;
                ViewData["hdMethod"] = "Edit";
                ViewData["jsAlert"] = "";
                ViewData["gNo"] = gNo;
                ViewData["FEmplNO"] = fEmplNO;

                BLL.HREmpl hREmplBLL = new BLL.HREmpl();
                Model.HREmpl empl = hREmplBLL.GetModel(Common.GlobalConstant.ENTITYNO, fEmplNO);
                hRWorkGrpMemberModel.FEmpName = empl.FEmpName;

            }
            else
            {
                ViewData["isVisible"] = true;
                ViewData["hdMethod"] = "ADD";
                ViewData["jsAlert"] = "";
                ViewData["gNo"] = gNo;
                ViewData["FEmplNO"] = "";
                hRWorkGrpMemberModel.FWorkGrpDutyNo = hRWorkGrpDutyList[0].FWorkGrpDutyNo;
            }
            return View(hRWorkGrpMemberModel);
        }
        /// <summary>
        /// 为工作组添加人
        /// </summary>
        /// <param name="hRWorkGrpMemberModel">工作组人员信息</param>
        /// <returns>返回工作组人员添加完成后视图</returns>
        public ActionResult SaveGroupEmpl(Model.HRWorkGrpMember hRWorkGrpMemberModel)
        {
            hRWorkGrpMemberModel.FWorkGrpNo = Fetch.Post("hbgNo");
            switch (Fetch.Post("hdMethod"))
            {
                case "ADD":
                    {
                        string existEmplNos = "";
                        string jsAlert = "";
                        BLL.HRWorkGrpMember workGrpMemberBll = new BLL.HRWorkGrpMember();
                        Model.HRWorkGrpMember model = null;
                        string[] results = Fetch.Post("hbFEmplNo").Split(',');
                        if (results.Length > 0)
                        {
                            try
                            {
                                List<Model.HRWorkGrpMember> members = new List<Model.HRWorkGrpMember>();
                                for (int i = 0; i < results.Length; i++)
                                {
                                    if (!workGrpMemberBll.Exists(Common.GlobalConstant.ENTITYNO, hRWorkGrpMemberModel.FWorkGrpNo, results[i]))
                                    {
                                        model = new Model.HRWorkGrpMember();
                                        model.FEntityNo = GlobalConstant.ENTITYNO;
                                        model.FWorkGrpNo = hRWorkGrpMemberModel.FWorkGrpNo;
                                        model.FEmplNo = results[i];
                                        model.FWorkGrpDutyNo = hRWorkGrpMemberModel.FWorkGrpDutyNo;
                                        model.FWorkDuty = hRWorkGrpMemberModel.FWorkDuty;
                                        model.FSeq = hRWorkGrpMemberModel.FSeq + i;
                                        members.Add(model);
                                    }
                                    else
                                    {
                                        existEmplNos += results[i] + ",";
                                    }
                                }

                                if (existEmplNos == "")
                                {
                                    workGrpMemberBll.Add(members);

                                    ViewData["RedirectURL"] = Url.Action("GroupEmplList", "Group", new { gNo = hRWorkGrpMemberModel.FWorkGrpNo, FEmplNO = hRWorkGrpMemberModel.FEmplNo });
                                    ViewData["IsParent"] = "N";
                                    return View("SuccessMessagePage");
                                }
                                else
                                {
                                    string emplNames = "";
                                    BLL.HREmpl emplBll = new BLL.HREmpl();
                                    string[] arrs = existEmplNos.Split(',');
                                    for (int j = 0; j < arrs.Length; j++)
                                    {
                                        Model.HREmpl empl = emplBll.GetModel(Common.GlobalConstant.ENTITYNO, arrs[j]);
                                        if (empl != null)
                                        {
                                            emplNames += empl.FEmpName + ",";
                                        }
                                    }
                                    emplNames = emplNames.Substring(0, emplNames.Length - 1);
                                    jsAlert = "成员：" + emplNames + "已存在该组中，不能重复添加！";
                                }

                            }
                            catch (Exception ex)
                            {
                                jsAlert = ex.Message;
                            }
                        }

                        else
                        {
                            jsAlert = "请先添加工作组成员！";
                        }

                        ViewData["isVisible"] = true;
                        ViewData["hdMethod"] = "ADD";
                        ViewData["jsAlert"] = jsAlert;
                        ViewData["gNo"] = Fetch.Post("hbgNo");
                        ViewData["FEmplNO"] = "";
                        return View("GroupEmplEdit", hRWorkGrpMemberModel);
                    }

                case "Edit":
                    {
                        string jsAlert = "";
                            try
                            {
                                BLL.HRWorkGrpMember workGrpMemberBll = new BLL.HRWorkGrpMember();
                                hRWorkGrpMemberModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                                hRWorkGrpMemberModel.FEmplNo = Fetch.Post("hbFEmplNo");
                                workGrpMemberBll.Update(hRWorkGrpMemberModel);
                                ViewData["RedirectURL"] = Url.Action("GroupEmplList", "Group", new { gNo = hRWorkGrpMemberModel.FWorkGrpNo, FEmplNO = hRWorkGrpMemberModel.FEmplNo });
                                ViewData["IsParent"] = "Y";
                                return View("SuccessMessageByTree");
                                
                            }
                            catch (Exception ex)
                            {
                                jsAlert = ex.Message;
                            }

                            BLL.HRWorkGrpMember hRWorkGrpMemberBLL = new BLL.HRWorkGrpMember();
                            hRWorkGrpMemberModel = hRWorkGrpMemberBLL.GetModel(Common.GlobalConstant.ENTITYNO, hRWorkGrpMemberModel.FWorkGrpNo, hRWorkGrpMemberModel.FEmplNo);
                            ViewData["isVisible"] = false;
                            ViewData["hdMethod"] = "Edit";
                            ViewData["jsAlert"] = "";
                            ViewData["gNo"] = hRWorkGrpMemberModel.FWorkGrpNo;
                            ViewData["FEmplNO"] = hRWorkGrpMemberModel.FEmplNo;
                            return View("GroupEmplEdit", hRWorkGrpMemberModel);
                    }
            }

            return View();
        }
        /// <summary>
        /// 保存工作组
        /// </summary>
        /// <param name="hRWorkGrpModel">工作组信息</param>
        /// <returns>返回工作组保存完成后视图</returns>
        public ActionResult SaveGroup(Model.HRWorkGrp hRWorkGrpModel)
        {
            hRWorkGrpModel.FEntityNo = Common.GlobalConstant.ENTITYNO;

            hRWorkGrpModel.FWorkGrpNo = Fetch.Post("FWorkGrpNo");
            List<Model.HRWorkGrpDuty> dutys = new List<Model.HRWorkGrpDuty>();
            string jsAlert = "";
            if (Fetch.Post("FDutyProp") != "")
            {
                BLL.HRWorkGrpDuty dutyBll = new BLL.HRWorkGrpDuty();
                Model.HRWorkGrpDuty duty = null;
                string existDuty = string.Empty;
                string[] dnos = Fetch.Post("FWorkGrpDutyNo").Split(',');
                string[] dnames = Fetch.Post("FDutyName").Split(',');
                string[] dprops = Fetch.Post("FDutyProp").Split(',');
                string[] dseqs = Fetch.Post("FSeq").Split(',');
                for (int i = 0; i < dprops.Length; i++)
                {
                    if (dnos[i].Trim() == "" || dnames[i].Trim() == "" || dseqs[i].Trim() == "")
                    {
                        continue;
                    }
                    //判断是否已经存在该岗位编号

                    if (!dutyBll.Exists(Common.GlobalConstant.ENTITYNO, hRWorkGrpModel.FWorkGrpNo, dnos[i].Trim()))
                    {
                        duty = new Model.HRWorkGrpDuty();
                        duty.FEntityNo = Common.GlobalConstant.ENTITYNO;
                        duty.FWorkGrpNo = hRWorkGrpModel.FWorkGrpNo;
                        duty.FWorkGrpDutyNo = dnos[i].Trim();
                        duty.FDutyName = dnames[i].Trim();
                        duty.FDutyProp = dprops[i];
                        duty.FSeq = Int32.Parse(dseqs[i].Trim());
                        dutys.Add(duty);
                    }
                    else
                    {
                        existDuty += dnames[i].Trim() + "  ";
                    }
                }



                try
                {
                    if (existDuty != "")
                    {
                        jsAlert = "其中：" + existDuty + " 工作组职务编号已存在！";
                    }
                    else
                    {
                        switch (Fetch.Post("hdMethod"))
                        {
                            case "ADD":
                                {
                                    BLL.HRWorkGrp workGrpBll = new BLL.HRWorkGrp();
                                    workGrpBll.AddWorkGroupAndDuty(hRWorkGrpModel, dutys);
                                    break;
                                }
                            case "Edit":
                                {
                                    BLL.HRWorkGrp workGrpBll = new BLL.HRWorkGrp();
                                    workGrpBll.UpdateWorkGroupAndDuty(hRWorkGrpModel, dutys);
                                    break;
                                }
                        }

                        ViewData["RedirectURL"] = Url.Action("Index", "Group");
                        ViewData["IsParent"] = "Y";
                        return View("SuccessMessagePage");
                    }
                }
                catch (Exception ex)
                {
                    jsAlert = ex.Message;
                }
            }
            else
            {
                if (Fetch.Post("hdMethod") == "Edit")
                {
                    BLL.HRWorkGrp workGrpBll = new BLL.HRWorkGrp();
                    workGrpBll.UpdateWorkGroupAndDuty(hRWorkGrpModel, dutys);
                    ViewData["RedirectURL"] = Url.Action("Index", "Group");
                    ViewData["IsParent"] = "Y";
                    return View("SuccessMessagePage");
                }
                else
                {
                    jsAlert = "必须增加岗位！";
                }

            }
            ViewData["jsAlert"] = jsAlert;
            switch (Fetch.Post("hdMethod"))
            {
                case "ADD":
                    {
                        ViewData["hdMethod"] = "ADD";
                        string _jsonstr = "";
                        _jsonstr += JsonConvert.SerializeObject(dutys);
                        ViewData["HRWorkGrpDutyNewList"] = _jsonstr;
                        ViewData["blemp"] = true;
                        break;
                    }
                case "Edit":
                    {
                        ViewData["hdMethod"] = "Edit";
                        string _jsonstr = "";
                        _jsonstr += JsonConvert.SerializeObject(dutys);
                        ViewData["HRWorkGrpDutyNewList"] = _jsonstr;
                        BLL.HRWorkGrpDuty workGrpDutyBll = new BLL.HRWorkGrpDuty();
                        List<Model.HRWorkGrpDuty> hRWorkGrpDutyList = workGrpDutyBll.GetModelList("FWorkGrpNo='" + hRWorkGrpModel.FWorkGrpNo + "' and FEntityNo='" + hRWorkGrpModel.FEntityNo + "' order by FSeq asc ");
                        string  _jsonstr2 = "";
                        _jsonstr2 += JsonConvert.SerializeObject(hRWorkGrpDutyList);
                        ViewData["HRWorkGrpDutyList"] = _jsonstr2;
                        ViewData["blemp"] = false;
                        break;
                    }
            }

            return View("GroupEdit", hRWorkGrpModel);
        }

        /// <summary>
        /// 人员树 
        /// </summary>
        /// <returns>返回人员选择树视图</returns>
        public ActionResult EmplTree()
        {
            BLL.HRDept deptBll = new BLL.HRDept();
            TreeModelList treeModelList = new TreeModelList();

            List<Model.HRDept> deptAllList = new List<Model.HRDept>();
            deptAllList = deptBll.GetListByEntityNo(GlobalConstant.ENTITYNO);
            if (deptAllList.Count > 0)
            {
                List<Model.HRDept> deptlist = (from c in deptAllList where c.FParentDeptNo == null orderby c.FSeq select c).ToList();

                foreach (Model.HRDept dept in deptlist)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = "dept|" + dept.FDeptNo,
                        Name = dept.FDeptName,
                        ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                    };

                    List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();
                    if (deptModelList.Count > 0)
                    {
                        GetChildGetChildDeptEmpl(deptModelList, treeParentItemModel, deptAllList);
                    }

                    GetEmpl(dept.FDeptNo, treeParentItemModel);

                    treeModelList.Add(treeParentItemModel);
                }

            }

            return View(treeModelList);
        }

        /// <summary>
        ///  递归获取部门数，绑定相关人员
        /// </summary>
        /// <param name="deptChildList">部门列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="deptAllList">部门集合</param>
        private void GetChildGetChildDeptEmpl(List<Model.HRDept> deptChildList, TreeItemModel treeParentItemModel, List<Model.HRDept> deptAllList)
        {
            foreach (Model.HRDept dept in deptChildList)
            {

                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = "dept|" + dept.FDeptNo,
                    Name = dept.FDeptName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();

                if (deptModelList.Count > 0)
                {
                    GetChildGetChildDeptEmpl(deptModelList, treeChildItemModel, deptAllList);
                }

                GetEmpl(dept.FDeptNo, treeChildItemModel);

            }

        }


        /// <summary>
        /// 获取人员数据
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        private void GetEmpl(string deptNo, TreeItemModel treeParentItemModel)
        {
            BLL.HREmplPosition emplBll = new BLL.HREmplPosition();
            DataSet ds = emplBll.GetDeptEmplList(deptNo, GlobalConstant.ENTITYNO);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = "user|" + row["FEmplNo"].ToString(),
                    Name = row["FEmpName"].ToString(),
                    ImageUrl = Url.Content("~/Content/ControlImages/user_male.png")
                };
            }
        }

       /// <summary>
       /// 删除工作组
       /// </summary>
       /// <param name="GroupNo">工作组编号</param>
        /// <returns>返回删除是否成功</returns>
        [HttpPost]
        public string DelGroup(string groupNo)
        {
            try
            {
                BLL.HRWorkGrp workGrpBll = new BLL.HRWorkGrp();
                workGrpBll.DeleteWorkGroupAndDuty(Common.GlobalConstant.ENTITYNO, groupNo);
                return "ok";
            }
            catch
            {
                return "error";
            }

        }


        /// <summary>
        /// 批量删除工作组
        /// </summary>
        /// <param name="groupNoArray">批量删除工作组编号</param>
        /// <returns>返回批量删除是否成功</returns>
        [HttpPost]
        public string DeleteManyGroup(string groupNoArray)
        {
            try
            {
                string[] groupNoArr = groupNoArray.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string groupNo in groupNoArr)
                {
                    BLL.HRWorkGrp workGrpBll = new BLL.HRWorkGrp();
                    workGrpBll.DeleteWorkGroupAndDuty(Common.GlobalConstant.ENTITYNO, groupNo);
                }
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        /// <summary>
        /// 删除工作组中的人员
        /// </summary>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fEmplNo">人员编号</param>
        /// <returns>删除是否成功</returns>
        public string DelGroupEmpl(string fWorkGrpNo, string fEmplNo)
        {

            try
            {
                BLL.HRWorkGrpMember workGrpMemberBll = new BLL.HRWorkGrpMember();
                workGrpMemberBll.Delete( Common.GlobalConstant.ENTITYNO, fWorkGrpNo, fEmplNo);
                return "ok";
            }
            catch
            {
                return "error";
            }
           
        }

   
        /// <summary>
        /// 批量删除工作组中人员
        /// </summary>
        /// <param name="fWorkGrpNoArr">工作组编号集合</param>
        /// <param name="fEmplNoArr">人员编号集合</param>
        /// <returns>批量删除是否成功</returns>
        public string EmplManyDel(string fWorkGrpNoArr, string fEmplNoArr)
        {

            try
            {
                string[] grpNoArr = fWorkGrpNoArr.Split(',');
                string[] emplNoArr = fEmplNoArr.Split(',');
                for (int i = 0; i < emplNoArr.Count(); i++)
                {
                    BLL.HRWorkGrpMember workGrpMemberBll = new BLL.HRWorkGrpMember();
                    workGrpMemberBll.Delete(Common.GlobalConstant.ENTITYNO, grpNoArr[i], emplNoArr[i]);
                }
                return "ok";
            }
            catch
            {
                return "error";
            }
           
        }

        

        /// <summary>
        /// 删除已增加的职位
        /// </summary>
        /// <param name="FWorkGrpNo">工作组编号</param>
        /// <param name="FWorkGrpDutyNo">职位编号</param>
        /// <returns>删除是否成功</returns>
        public string DelDuty(string fWorkGrpNo, string fWorkGrpDutyNo)
        {
            BLL.HRWorkGrpDuty workGrpDutyBll = new BLL.HRWorkGrpDuty();
            if (workGrpDutyBll.Exists(Common.GlobalConstant.ENTITYNO, fWorkGrpNo, fWorkGrpDutyNo))
            {
                workGrpDutyBll.Delete(Common.GlobalConstant.ENTITYNO, fWorkGrpNo, fWorkGrpDutyNo);
                return "ok";
            }
            else
            {
                return "error";
            }
        }


    }
}
